﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KimPhongCRM.Models;
using KimPhongCRM.Lib;

namespace KimPhongCRM.Controllers
{
    [Authorize]
    public class BaseController : Controller
    {
        Entities db1 = new Entities();
        //Active
        public JsonResult ToogleActive(string keyvalue, bool active, string tablename, string keyname)
        {

            Dictionary<string, object> dic = new Dictionary<string, object>();
            if (!string.IsNullOrEmpty(keyvalue))
            {
                try
                {
                    string editor = SessionData.GetSession("sdUserId").ToString();
                    int result = db1.sp_Toggle_Active(active, tablename, keyname, keyvalue, editor);
                    dic.Add("result", result > 0);
                    dic.Add("key", keyvalue);
                    dic.Add("active", active);
                }
                catch (Exception e)
                {

                }
            }
            return Json(dic, JsonRequestBehavior.AllowGet);
        }
        //Order
        public JsonResult Reorder(int keyvalue, string tablename, string keyname, string ordertype)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            if (keyvalue != 0)
            {
                try
                {
                    int result = db1.sp_Reorder(ordertype, keyvalue, tablename, keyname);
                    dic.Add("result", result > 0);
                    dic.Add("key", keyvalue);
                    dic.Add("order", ordertype);
                }
                catch (Exception e)
                {

                }
            }
            return Json(dic, JsonRequestBehavior.AllowGet);
        }
        public JsonResult CustomReorder(int order, int keyvalue, string tablename, string keyname)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            if (keyvalue != 0)
            {
                try
                {
                    int result = db1.sp_Change_Order(order, keyvalue, tablename, keyname);
                    dic.Add("result", result > 0);
                    dic.Add("key", keyvalue);
                    dic.Add("order", order);
                }
                catch (Exception e)
                {

                }
            }
            return Json(dic, JsonRequestBehavior.AllowGet);
        }
        //Check user id login
        public void ressUserID(string username)
        {
            Guid? user_id = db1.sp_Select_UserId_ByUserName(username).FirstOrDefault();
            SessionData.StartSession("sdUserId", user_id);
        }
    }
}
